package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.CommonResult;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 *
 * @author 李红磊
 * @since 2021-06-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin//解决跨域访问
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;


    @GetMapping("/test")//查询所有
    public CommonResult test1(){
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        List<EduTeacher> list = teacherService.list(wrapper);
        return CommonResult.ok().data("items",list);
    }



    @DeleteMapping("/del/{id}") //逻辑删除
    public CommonResult logicDel(@PathVariable(value = "id",required = true)String id){
        boolean flag = teacherService.removeById(id);
       /* if(flag){
            return CommonResult.ok();
        }
        return CommonResult.error();*/

        return flag?CommonResult.ok():CommonResult.error();
    }




    @GetMapping("/pageTeacher/{cur}/{siz}")//分页查询教师
    public CommonResult pageTeacher(@PathVariable("cur")Long cur,
                                    @PathVariable("siz")Long siz){
        Page<EduTeacher> page = new Page<>(cur,siz);
        //调用方法实现分页
        teacherService.page(page,null);

        long total = page.getTotal();//总纪录数
        List<EduTeacher> records = page.getRecords();//数据

        HashMap<Object, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);

        return CommonResult.ok().data("map",map);
    }



    @PostMapping("/condition/{current}/{size}")//条件查询结合分页查询讲师
    public CommonResult conditionQ(@PathVariable("current")Long current,
                                   @PathVariable("size")Long size,
                                   @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建paage
        Page<EduTeacher> page = new Page<>(current,size);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        //判断条件是否为空，若为空则不添加
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if(!(StringUtils.isEmpty(name))){//类似于动态sql
            wrapper.like("name",name);
        }
        if(!(StringUtils.isEmpty(level))){
            wrapper.eq("level",level);
        }
        if(!(StringUtils.isEmpty(begin))){
            wrapper.ge("gmt_create",begin);
        }
        if(!(StringUtils.isEmpty(end))){
            wrapper.le("gmt_create",end);
        }

        teacherService.page(page, wrapper);//调用方法执行分页查询
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);

        return CommonResult.ok().data("map",map);

    }


    @PostMapping("/addTeacher")//添加讲师
    public CommonResult addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        return flag?CommonResult.ok():CommonResult.error();
    }

    @GetMapping("/queryTeacher/{id}")//根据id查询讲师
    public CommonResult queryTeacher(@PathVariable("id")String id){
        EduTeacher teacher = teacherService.getById(id);
        return teacher==null?CommonResult.error():CommonResult.ok().data("teacher",teacher);
    }


    @PostMapping("/updateTeacher")//修改讲师
    public CommonResult updateTeacher(@RequestBody EduTeacher eduTeacher){
       return teacherService.updateById(eduTeacher)?CommonResult.ok():CommonResult.error();

    }



    

}

